package com.hyg.common.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.hyg.common.CommonLibary;

import java.lang.reflect.Type;
import java.util.Set;


/**
 * Author: huangyuguang
 * Date: 2022/5/5
 */
public class SPUtils {
    public static SharedPreferences getPreferences(Context context) {
        return context.getSharedPreferences(context.getApplicationContext().getPackageName(), Context.MODE_PRIVATE);
    }

    public static <T> T getObject(Class<T> clazz) {
        String key = getKey(clazz);
        String json = getString(key, null);
        if (TextUtils.isEmpty(json)) {
            return null;
        }
        try {
            Gson gson = new Gson();
            return gson.fromJson(json, clazz);
        } catch (Exception e) {
            return null;
        }
    }

    // 通过Type去获取一个泛型对象
    public static <T> T getObject(Type type) {
        String key = getKey(type);
        String json = getString(key, null);
        if (TextUtils.isEmpty(json)) {
            return null;
        }
        try {
            Gson gson = new Gson();
            return gson.fromJson(json, type);
        } catch (Exception e) {
            return null;
        }
    }

    public static void putObject(Object object) {
        String key = getKey(object.getClass());
        Gson gson = new Gson();
        String json = gson.toJson(object);
        putString(key, json);
    }

    /**
     * 保存一个泛型对象
     *
     * @param object
     * @param type    如果你要保存 List<Person> 这个类, type应该 传入 new TypeToken<List<Person>>() {}.getType()
     */
    public static void putObject(Object object, Type type) {
        String key = getKey(type);
        Gson gson = new Gson();
        String json = gson.toJson(object);
        putString(key, json);
    }

    public static void removeObject(Class<?> clazz){
        remove(getKey(clazz));
    }

    public static String getKey(Class<?> clazz) {
        return clazz.getName();
    }

    public static String getKey(Type type) {
        return type.toString();
    }

    public static void remove(String key) {
        SharedPreferences sp = getPreferences(CommonLibary.getContext());
        SharedPreferences.Editor edit = sp.edit();
        edit.remove(key);
        edit.commit();
    }

    public static void putBoolean(String key, boolean value) {
        SharedPreferences sp = getPreferences(CommonLibary.getContext());
        SharedPreferences.Editor edit = sp.edit();
        edit.putBoolean(key, value);
        edit.commit();
    }

    public static boolean getBoolean(String key, boolean defValue) {
        return getPreferences(CommonLibary.getContext()).getBoolean(key, defValue);
    }

    public static void putString(String key, String value) {
        SharedPreferences sp = getPreferences(CommonLibary.getContext());
        SharedPreferences.Editor edit = sp.edit();
        edit.putString(key, value);
        edit.commit();
    }

    public static String getString(String key, String defValue) {
        return getPreferences(CommonLibary.getContext()).getString(key, defValue);
    }

    public static void putInt(String key, int value) {
        SharedPreferences sp = getPreferences(CommonLibary.getContext());
        SharedPreferences.Editor edit = sp.edit();
        edit.putInt(key, value);
        edit.commit();
    }

    public static int getInt(String key, int defValue) {
        return getPreferences(CommonLibary.getContext()).getInt(key, defValue);
    }

    public static void putLong(String key, long value) {
        SharedPreferences sp = getPreferences(CommonLibary.getContext());
        SharedPreferences.Editor edit = sp.edit();
        edit.putLong(key, value);
        edit.commit();
    }

    public static long getLong(String key, long defValue) {
        return getPreferences(CommonLibary.getContext()).getLong(key, defValue);
    }

    public static void putFloat(String key, float value) {
        SharedPreferences sp = getPreferences(CommonLibary.getContext());
        SharedPreferences.Editor edit = sp.edit();
        edit.putFloat(key, value);
        edit.commit();
    }

    public static float getFloat(String key, float defValue) {
        return getPreferences(CommonLibary.getContext()).getFloat(key, defValue);
    }

    public static void putStringSet(String key, Set<String> value) {
        SharedPreferences sp = getPreferences(CommonLibary.getContext());
        SharedPreferences.Editor edit = sp.edit();
        edit.putStringSet(key, value);
        edit.commit();
    }

    public static Set<String> getStringSet(String key, Set<String> defValue) {
        return getPreferences(CommonLibary.getContext()).getStringSet(key, defValue);
    }
}
